#define WIN32_LEAN_AND_MEAN

#define DIRECTINPUT_VERSION 0x0900

#include <windows.h>
#include <windowsx.h> 
#include <mmsystem.h>
#include <iostream>
#include <conio.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h> 
#include <math.h>
#include <io.h>
#include <fcntl.h>
#include <tchar.h>

#include <ddraw.h>  // directX includes
#include "data_structure.h"

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

HWND main_window_handle           = NULL; // save the window handle

Poly4D poly;
Camera4D camera;
Vector4D camera_pos;
Vector4D camera_dir;

void initialize();
void tick();
void dispose();

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
    // Register the window class.
    const wchar_t CLASS_NAME[]  = L"Sample Window Class";
    
    WNDCLASS wc = { };

    wc.lpfnWndProc   = WindowProc;
    wc.hInstance     = hInstance;
    wc.lpszClassName = CLASS_NAME;
	wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	

    RegisterClass(&wc);

    // Create the window.

    HWND hwnd = CreateWindowEx(
        0,                              // Optional window styles.
        CLASS_NAME,                     // Window class
        L"Learn to Program Windows",    // Window text
        WS_OVERLAPPEDWINDOW,            // Window style

        // Size and position
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,

        NULL,       // Parent window    
        NULL,       // Menu
        hInstance,  // Instance handle
        NULL        // Additional application data
        );
	
    if (hwnd == NULL)
    {
        return 0;
    }

	main_window_handle = hwnd;

    ShowWindow(hwnd, nCmdShow);
	initialize();
    // Run the message loop.

    MSG msg = { };
	while(1){
		if(PeekMessage(&msg , NULL , 0 , 0 , PM_REMOVE)){
			if(msg.message == WM_QUIT){
				break;
			}
		}

		TranslateMessage(&msg);

		DispatchMessage(&msg);

		tick();
	}

    //while (GetMessage(&msg, NULL, 0, 0))
    //{
    //    TranslateMessage(&msg);
    //    DispatchMessage(&msg);
    //}

    return 0;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;

    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hwnd, &ps);

            FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1));

            EndPaint(hwnd, &ps);
        }
        return 0;

    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}



void initialize(){
	poly.verties[0].x = 50;
	poly.verties[0].y = 0;
	poly.verties[0].z = 50;
	poly.verties[0].w = 1;

	poly.verties[1].x = 50;
	poly.verties[1].y = 0;
	poly.verties[1].z = -50;
	poly.verties[1].w = 1;

	poly.verties[2].x = 0;
	poly.verties[2].y = 100;
	poly.verties[2].z = 0;
	poly.verties[2].w = 1;
	
	InitCamera4D(&camera , &camera_pos , &camera_dir , NULL , 50 , 100 , 90 , 500 , 500);
}

void tick(){
	
}